﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;

namespace BLL
{
    /// <summary>
    /// 获取Type数据保存的位置，并实现保存、读取、添加、更新、删除的功能
    /// </summary>
    public class TypeManagement
    {
        private TypeCollection typeList = new TypeCollection();
        public TypeManagement()
        {
        }
        /// <summary>
        /// 获取type文件夹的绝对路径(type文件夹中保存着type.xml)
        /// </summary>
        public static string DirOfTypeListFile
        {
            get
            {
                string dir = Application.StartupPath + "\\type";
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                return dir;
            }
        }
        /// <summary>
        /// 获取type.xml文件的绝对路径
        /// </summary>
        public static string PathOfTypeListFile
        {
            get
            {
                string path = DirOfTypeListFile + "\\type.xml";
                if (!File.Exists(path))
                {
                    FileStream fs = null;
                    try
                    {
                        fs = File.Create(path);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        if (fs != null)
                        {
                            fs.Close();
                        }
                    }
                }
                return path;
            }
        }
        /// <summary>
        /// 获取或设置记录列表
        /// </summary>
        public TypeCollection TypeList
        {
            get
            {
                return typeList;
            }
            set
            {
                typeList = value;
            }
        }

        /// <summary>
        /// 将数据保存到type.xml
        /// </summary>
        public bool SaveDataToFile()
        {
            StreamWriter sw = new StreamWriter(TypeManagement.PathOfTypeListFile, false, Encoding.Default);
            XmlSerializer xml = new XmlSerializer(typeof(TypeCollection));
            try
            {
                xml.Serialize(sw, typeList);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
            finally
            {
                if (sw != null)
                {
                    sw.Close();
                }
            }

            return true;
        }

        /// <summary>
        /// 从type.xml中读取数据
        /// </summary>
        public bool LoadDataFromFile()
        {
            StreamReader sr = new StreamReader(TypeManagement.PathOfTypeListFile, Encoding.Default);
            XmlSerializer xml = new XmlSerializer(typeof(TypeCollection));
            try
            {
                typeList = (TypeCollection)xml.Deserialize(sr);
            }
            catch //(Exception ex)
            {
                //MessageBox.Show(ex.ToString());
                return false;
            }
            finally
            {
                if (sr != null)
                {
                    sr.Close();
                }
            }

            return true;
        }

        /// <summary>
        /// 将一条新记录保存入type.xml文件和TypeManagement.TypetList中
        /// </summary>
        ///<param name="typ" Type类的对象></param>
        public void AddType(Type typ)
        {
            this.TypeList.Add(typ);
            this.SaveDataToFile();
        }
        /// <summary>
        /// 将该ID的记录从type.xml文件和TypeManagement.TypeList中删除
        /// </summary>
        ///<param name="iID" 该账户对应ID></param>
        public void DelType(int iID)
        {
            foreach (Type type in this.TypeList)
            {
                if (type.ID == iID)
                {
                    this.TypeList.Remove(type);
                    break;     //每次删除操作都会影响TypeList，导致无法继续foreach循环，因此每次只能执行一次删除操作
                }
            }
            this.SaveDataToFile();
        }

        /// <summary>
        /// 将该ID的记录在type.xml文件和TypeManagement.TypeList中更新
        /// </summary>
        ///<param name="typ" Type类的对象></param>
        public void UpdateType(Type typ)
        {
            int icount = 0, iwhere = 0;
            foreach (Type type in this.TypeList)
            {
                if (type.ID == typ.ID)
                {
                    iwhere = icount;
                    this.TypeList[iwhere].ID = typ.ID;
                    this.TypeList[iwhere].ID = typ.ID;
                    this.TypeList[iwhere].Isincome= typ.Isincome;
                    this.TypeList[iwhere].Name = typ.Name;
                    this.SaveDataToFile();
                    break;     //每次删除操作都会影响TypeList，导致无法继续foreach循环，因此每次只能执行一次删除操作
                }
                icount++;
            }

        }

        public Type GetType(int iID)
        {
            foreach (Type type in this.TypeList)
            {
                if (type.ID == iID)
                {
                    return type;
                }
            }
            return null;
        }
    }
}
